package cn.edu.tju.elm.controller;

import cn.edu.tju.core.model.Review;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.edu.tju.elm.service.ReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/review")
public class ReviewController {

    @Autowired
    private ReviewService reviewService;

    /**
     * 添加评价
     */
    @PostMapping("/add")
    public String addReview(@RequestBody Review review) {
        boolean result = reviewService.save(review);
        return result ? "添加成功" : "添加失败";
    }

    /**
     * 根据ID删除评价
     */
    @DeleteMapping("/delete/{id}")
    public String deleteReview(@PathVariable Integer id) {
        boolean result = reviewService.removeById(id);
        return result ? "删除成功" : "删除失败";
    }

    /**
     * 更新评价
     */
    @PutMapping("/update")
    public String updateReview(@RequestBody Review review) {
        boolean result = reviewService.updateById(review);
        return result ? "更新成功" : "更新失败";
    }

    /**
     * 根据ID查询评价
     */
    @GetMapping("/get/{id}")
    public Review getReviewById(@PathVariable Integer id) {
        return reviewService.getById(id);
    }

    /**
     * 查询所有评价（分页）
     */
    @GetMapping("/list")
    public Page<Review> getReviewList(@RequestParam(defaultValue = "1") Integer page,
                                      @RequestParam(defaultValue = "10") Integer size) {
        return reviewService.page(new Page<>(page, size));
    }

    /**
     * 根据用户ID查询评价
     */
    @GetMapping("/user/{userId}")
    public List<Review> getReviewsByUserId(@PathVariable Integer userId) {
        QueryWrapper<Review> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        return reviewService.list(queryWrapper);
    }

    /**
     * 根据订单ID查询评价
     */
    @GetMapping("/order/{orderId}")
    public List<Review> getReviewsByOrderId(@PathVariable Integer orderId) {
        QueryWrapper<Review> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", orderId);
        return reviewService.list(queryWrapper);
    }

    /**
     * 根据评分查询评价
     */
    @GetMapping("/rating/{rating}")
    public List<Review> getReviewsByRating(@PathVariable Integer rating) {
        QueryWrapper<Review> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("rating", rating);
        return reviewService.list(queryWrapper);
    }
}
